import json
import subprocess
from Fingerprint.util import *
from multiprocessing import Pool
import os

mad_command = 'python MAD.py %s'
fingerprint_command = 'python finger_print.py %s %s'


def call_fingerprint(args):
    print('正在生成%s的指纹' % args)
    process = subprocess.Popen((fingerprint_command % (args, para_json)),
                               shell=True)
    output, error = process.communicate()

    return output, error


def call_mad(params):
    print('开始计算平均值和MAD')
    process = subprocess.Popen((mad_command % params), stdout=subprocess.PIPE, shell=True)
    output, error = process.communicate()
    return output, error


if __name__ == '__main__':
    # 读取json
    para_json = '../data/fp_input_CI_CDY_EHZ.json'
    params = get_params(para_json)
    print(params)
    print(params['fingerprint']['sampling_rate'])

    # 预处理阶段计算MAD
    print((call_mad(params)))

    # 生成fingerprint
    files = ['../data/Deci5.Pick.19991015130000.CI.CDY.EHZ.sac']
    p = Pool(min(params['performance']['num_fp_thread'], len(files)))
    p.map(call_fingerprint, files)

    # 合并fp文件
    nfp = 0
    ntimes = get_ntimes(params)
    fp_in_bytes = params['fingerprint']['nfreq'] * ntimes / 4
    print(fp_in_bytes)
    fp_path,ts_path = get_fp_ts_folders(params)
    print(fp_path)
    print(ts_path)
    final_fp_name = '%s%s' % (fp_path,get_combined_fp_name(params))
    if os.path.exists(final_fp_name):
        os.remove(final_fp_name)
    print('合并生成最终的fingerprint文件%s'%final_fp_name)

    final_ts_name = '%s%s' % (ts_path, get_combined_ts_name(params))
    if os.path.exists(final_ts_name):
        os.remove(final_ts_name)
    print('合并生成最终的timestamp文件%s' % final_ts_name)

    for fname in files:
        fp_file = fp_path + get_fp_fname(fname[8:])
        os.system('cat %s >> %s' % (fp_file,final_fp_name))

        ts_file = ts_path + get_ts_fname(fname[8:])
        os.system('cat %s >> %s' % (ts_file, final_ts_name))

        # 验证fingerprint数量
        num_lines = sum(1 for line in open(ts_file))
        nfp += num_lines
        fsize = os.path.getsize(fp_file)
        if fsize / fp_in_bytes != num_lines:
            print("Exception: # fingerprints in %s don't match" % fname[8:])
            print("Fingerprint file: %d, timestamp file: %d" %(fsize / fp_in_bytes, num_lines))
            exists(1)

    fsize = os.path.getsize(final_fp_name)
    print('fp文件大小：%d bytes' % (fsize))
    print('# fp行数： %d' %(nfp))
    ndim = fsize * 8 / nfp

    # 保存fp
    save_fp_stats(params,nfp,ndim)


